﻿Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO



Public Class FormPengurus
    Inherits PageTemplate
    Dim vKETUA As String = ""
    Dim vTELEPON As String = ""
    Dim vPETUGAS1 As String = ""
    Dim vTELEPON1 As String = ""
    Dim vPETUGAS2 As String = ""
    Dim vTELEPON2 As String = ""
    Dim vKETERANGAN As String = ""
    Public filename As String = ""
    Dim cani As Boolean = False
    Dim canu As Boolean = False
    Dim cand As Boolean = False
    Private Sub FormPengurus_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        load_data()
        Dim dr() As DataRow
        If Utils.RowExist(LocalSession.dtrole) Then
            dr = LocalSession.dtrole.Select("menuid = '" & LocalSession.PublicMenuId & "'")
            If dr.Length > 0 Then
                '       dt = dr.CopyToDataTable
                cani = IIf(IsDBNull(dr(0)("to_insert")), 0, dr(0)("to_insert"))
                canu = IIf(IsDBNull(dr(0)("to_update")), 0, dr(0)("to_update"))
                cand = IIf(IsDBNull(dr(0)("to_delete")), 0, dr(0)("to_delete"))
            End If
        End If
        If Not cani Then
            SimpleButton1.Enabled = False
        ElseIf Not canu Then
            SimpleButton1.Enabled = False
        End If
    End Sub

    Private Sub load_data()
        Dim dtpengurus As New DataTable
        Dim strQuery As String()
        Dim err As String = ""
        Dim imagebaru As Image
        imagebaru = Nothing
        strQuery = New String() {"*", "datapengurus", "id", "1"}
        If Utils.executeSP("proc_searchall", strQuery, dtpengurus) Then
            If Utils.RowExist(dtpengurus) Then
                txtKETUA.Text = IIf(IsDBNull(dtpengurus(0)(1)), "", dtpengurus(0)(1))

                txtTELEPON.Text = IIf(IsDBNull(dtpengurus(0)(2)), "", dtpengurus(0)(2))
                txtPETUGAS1.Text = IIf(IsDBNull(dtpengurus(0)(3)), "", dtpengurus(0)(3))
                txtTELEPON1.Text = IIf(IsDBNull(dtpengurus(0)(4)), "", dtpengurus(0)(4))
                txtPETUGAS2.Text = IIf(IsDBNull(dtpengurus(0)(5)), "", dtpengurus(0)(5))
                txtTELEPON2.Text = IIf(IsDBNull(dtpengurus(0)(6)), "", dtpengurus(0)(6))
                meKETERANGAN.Text = IIf(IsDBNull(dtpengurus(0)(7)), "", dtpengurus(0)(7))

                If Not IsDBNull(dtpengurus(0)("foto")) Then
                    Byte2Image(imagebaru, dtpengurus(0)("foto"))
                    PictureBox1.Image = imagebaru
                End If
            End If
            
            
        End If
    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Dim datakosong As String = ""
        'If txtKETUA.Text = "" Then
        '    datakosong = "Dengan Beberapa Data Kosong"
        'ElseIf txtTELEPON.Text = "" Then
        '    datakosong = "Dengan Beberapa Data Kosong"
        'ElseIf txtPETUGAS1.Text = "" Then
        '    datakosong = "Dengan Beberapa Data Kosong"
        'ElseIf txtTELEPON1.Text = "" Then
        '    datakosong = "Dengan Beberapa Data Kosong"
        'ElseIf txtPETUGAS2.Text = "" Then
        '    datakosong = "Dengan Beberapa Data Kosong"
        'ElseIf txtTELEPON2.Text = "" Then
        '    datakosong = "Dengan Beberapa Data Kosong"
        'ElseIf meKETERANGAN.Text = "" Then
        '    datakosong = "Dengan Beberapa Data Kosong"
        '    'Else
        '    '    Dim GBRBYTE As Byte()
        '    '    GBRBYTE = imagetobyte(filename)
        '    '    If GBRBYTE Is Nothing Then
        '    '        datakosong = "Dengan Beberapa Data Kosong"
        '    '    End If
        'End If

        Dim err As String = ""
        inisial()
        Dim dtResulExecute As New DataTable
        Dim ValResult As String = ""

        Dim bf As New BinaryFormatter
        Dim ms As New MemoryStream
        Dim bytearr As Byte()
        Dim vmemo As String = String.Empty
        If meKETERANGAN.Lines.Count > 0 Then
            ms = New MemoryStream
            bf.Serialize(ms, meKETERANGAN.Lines)
            bytearr = ms.ToArray

            For i As Integer = 0 To meKETERANGAN.Lines.Count - 1
                vmemo += meKETERANGAN.Lines(i).ToString
                If i < meKETERANGAN.Lines.Count - 1 Then
                    vmemo += "\n"
                End If
            Next
            vKETERANGAN = vmemo
        Else
        End If
        datakosong = ""
        datakosong &= "Ketua" & vbTab & ": " & txtKETUA.Text & vbCrLf
        datakosong &= "Telepon" & vbTab & ": " & txtTELEPON.Text & vbCrLf
        datakosong &= "Petugas1" & vbTab & ": " & txtPETUGAS1.Text & vbCrLf
        datakosong &= "Telepon1" & vbTab & ": " & txtTELEPON1.Text & vbCrLf
        datakosong &= "Petugas2" & vbTab & ": " & txtPETUGAS2.Text & vbCrLf
        datakosong &= "Telepon2" & vbTab & ": " & txtTELEPON2.Text & vbCrLf
        datakosong &= "Keterangan" & " : " & meKETERANGAN.Text & vbCrLf
        Dim GBRBYTE As Byte()
        GBRBYTE = imagetobyte(filename)
        If GBRBYTE Is Nothing Then
            datakosong &= "Foto" & vbTab & ": " & "Tidak Ada Foto"
        Else
            datakosong &= "Foto" & vbTab & ": " & "Ada Foto"
        End If


        If MsgBox("Apakah Anda Akan Merubah Data Pengurus UKS " & vbCrLf & datakosong, MsgBoxStyle.OkCancel, "Informasi") = MsgBoxResult.Ok Then
            SimpanData("update")
            load_data()
        End If

    End Sub

    Private Sub inisial()
        vKETUA = txtKETUA.Text
        vTELEPON = txtTELEPON.Text
        vPETUGAS1 = txtPETUGAS1.Text
        vTELEPON1 = txtTELEPON1.Text
        vPETUGAS2 = txtPETUGAS2.Text
        vTELEPON2 = txtTELEPON2.Text
        vKETERANGAN = meKETERANGAN.Text
    End Sub

    Private Sub SimpanData(ByVal jnsSimpan As String)
        Dim strQuery As Object()
        Dim err As String = ""
        inisial()
        Dim dtResulExecute As New DataTable
        '        Dim MsgResult As String = ""
        Dim ValResult As String = ""
        Dim GBRBYTE As Byte()
        GBRBYTE = imagetobyte(filename)

        strQuery = New Object() {replaceInput(vKETUA), replaceInput(vTELEPON), replaceInput(vPETUGAS1), replaceInput(vTELEPON1), replaceInput(vPETUGAS2), _
                                 replaceInput(vTELEPON2), replaceInput(vKETERANGAN), IIf(GBRBYTE Is Nothing, Convert.DBNull, GBRBYTE)}
        If Utils.executeSP("proc_pengurus", strQuery, dtResulExecute) Then
            If Utils.RowExist(dtResulExecute) Then
                Dim MsgResult As String = "Execute Successfull"

                If MsgResult = dtResulExecute(0)(0) Then
                    MsgBox(MsgResult, MsgBoxStyle.Information, "Success")
                Else
                    MsgBox(dtResulExecute(0)(0), MsgBoxStyle.Critical, "Error")
                End If
            End If
        End If

    End Sub

    Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte)
        '
        Dim ImageStream As MemoryStream
        Try

            If ByteArr.GetUpperBound(0) > 0 Then
                ImageStream = New MemoryStream(ByteArr)
                NewImage = Image.FromStream(ImageStream)
            Else
                NewImage = Nothing
            End If

        Catch ex As Exception

            NewImage = Nothing

        End Try

    End Sub

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        OpenFileDialog1.InitialDirectory = "c:\"
        OpenFileDialog1.Filter = "JPEG (*.jpg) |*.jpg;*.jpe;*.jpeg | PNG (*.png)|*.png;*.png | GIF (*.gif)|*.gif;*.gif | BMP (*.bmp)|*.bmp;*.bmp |All files (*.*)|*.*" '"Word (*.doc) |*.doc;*.rtf|(*.txt) |*.txt|(*.*) |*.*" 

        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True
        OpenFileDialog1.FileName = ""
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            filename = OpenFileDialog1.FileName
            Dim fz As UInt32 = 0
            Dim fs As IO.MemoryStream
            Try
                fs = New IO.MemoryStream(IO.File.ReadAllBytes(filename))
                fz = fs.Length
                If fz > 500000 Then
                    MsgBox("Maaf Maksimal Ukuran File >= 1Mb(1.000.000 byte)")
                    Exit Sub
                End If
            Catch ex As Exception

            End Try

            PictureBox1.ImageLocation = filename
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            PictureBox1.Refresh()

        End If
    End Sub

    Private Function imagetobyte(ByVal strGambar As String) As Byte()
        Dim imageArray As Byte()
        If strGambar = "" Then

            Dim originalImage As Image = PictureBox1.Image
            If originalImage IsNot Nothing Then
                Using stream As IO.MemoryStream = New IO.MemoryStream()
                    originalImage.Save(stream, Imaging.ImageFormat.Jpeg)
                    imageArray = stream.ToArray
                End Using

            Else
                imageArray = Nothing
            End If
        Else
            imageArray = IO.File.ReadAllBytes(strGambar)
        End If
        Return imageArray
    End Function

    Private Sub txtTELEPON_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTELEPON.KeyPress
        e.Handled = Utils.TelpValidasi(Asc(e.KeyChar))
    End Sub

    Private Sub txtTELEPON1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTELEPON1.KeyPress
        e.Handled = Utils.TelpValidasi(Asc(e.KeyChar))
    End Sub

    Private Sub txtTELEPON2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTELEPON2.KeyPress
        e.Handled = Utils.TelpValidasi(Asc(e.KeyChar))
    End Sub

    Private Function replaceInput(ByVal teks As String) As String
        teks = teks.Replace("\", "\\")
        teks = teks.Replace("'", "\'")
        Return teks
    End Function
End Class
